package com.landscope.issue.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.auth0.jwt.interfaces.Claim;
import com.landscope.issue.bean.E_PERMISSION_TYPE;
import com.landscope.issue.controller.api.dto.*;
import com.landscope.issue.controller.api.param.LoginParam;
import com.landscope.issue.controller.api.param.MainIndexParam;
import com.landscope.issue.controller.base.ResponseResult;
import com.landscope.issue.entity.Monitordata;
import com.landscope.issue.entity.Project;
import com.landscope.issue.entity.User;
import com.landscope.issue.exception.BizException;
import com.landscope.issue.exception.E_EXCEPTION_TYPE;
import com.landscope.issue.mapper.AlarmmsgMapper;
import com.landscope.issue.mapper.MonitordataMapper;
import com.landscope.issue.mapper.ProjectMapper;
import com.landscope.issue.mapper.StainfoMapper;
import com.landscope.issue.utils.ImageUtil;
import com.landscope.issue.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


/**
 * @author qzp
 * @version v1.0
 * @date 2021/8/2 15:25
 **/
@Service
public class ApiMainService {

    @Value("${token_valid}")
    private Boolean tokenValid;

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private StainfoMapper stainfoMapper;
    @Resource
    private MonitordataMapper monitordataMapper;
    @Resource
    private AlarmmsgMapper alarmmsgMapper;
    @Resource
    private ImageUtil imageUtil;

    public ResponseResult<MainIndexDto> index(MainIndexParam param) {
        MainIndexDto mainIndexDto = new MainIndexDto();
        String[] codes;
        if (tokenValid) {
            mainIndexDto.setUserName(TokenUtil.getUserName());
            // 验证token
            Integer userId = TokenUtil.verifyToken();
            // 验证项目权限
            User user = TokenUtil.verifyAuthority(param.getAccessToken(), param.getProjectCode());
            codes = user.getManageProjectCodes().split(";");
            // 验证功能权限
            TokenUtil.verifyRole(E_PERMISSION_TYPE.jcad_en.toString());
            // 验证功能权限
            TokenUtil.verifyRole(E_PERMISSION_TYPE.stad_en.toString());
        } else {
            List<SimProjectListDto> projectList = projectMapper.findAllSimple();
            codes = new String[projectList.size()];
            for (int i = 0; i < projectList.size(); i++) {
                codes[i] = projectList.get(i).getProjectCode();
            }
        }

        List<ProjectListDto> projectList = projectMapper.findByCodes(codes);

        if (projectList.size() == 0)
            return new ResponseResult().success();

        if (StrUtil.isEmpty(param.getProjectCode()))
            param.setProjectCode(projectList.get(0).getProjectCode());

        // 获取监测项目列表
        Project project = projectMapper.findByCode(param.getProjectCode());
        MainIndexDto.MonitoringSite monitoringSite = new MainIndexDto.MonitoringSite();
        monitoringSite.setImageUrl(imageUtil.formatImageUrl(project.getProjectMap()));
        monitoringSite.setList(projectList);

        mainIndexDto.setTitle(project.getProjectSoftTittle());

        // 获取台站地图列表
        MainIndexDto.StaMap staMap = new MainIndexDto.StaMap();
        staMap.setImageUrl(imageUtil.formatImageUrl(project.getProjectStationMap()));
        staMap.setCode(project.getCode());
        staMap.setName(project.getName());
        List<StaListDto> staList = stainfoMapper.findByCode(param.getProjectCode());
        if (StrUtil.isNotEmpty(project.getProjectXysta())) {
            String[] strs = project.getProjectXysta().split(";");
            if (strs.length > 0)
                for (StaListDto dto : staList) {
                    for (String key : strs) {
                        if (StrUtil.isNotEmpty(key)) {
                            String[] ps = key.split("_");
                            if (ps.length == 3)
                                if (dto.getStaId().intValue() == Integer.valueOf(ps[0]).intValue()) {
                                    dto.setX(Float.valueOf(ps[1]));
                                    dto.setY(Float.valueOf(ps[2]));
                                }
                        }
                    }
                }
        }
        staMap.setList(staList);

        // 获取台站实时列表信息
        List<StaListDataDto> staDataList = stainfoMapper.findDataListByCode(param.getProjectCode());
        for (StaListDataDto dto : staDataList) {
            // 经纬度保留两位小数
            dto.setLatitude(dto.getLatitude() == null ? "" : new BigDecimal(dto.getLatitude()).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
            dto.setLongitude(dto.getLongitude() == null ? "" : new BigDecimal(dto.getLongitude()).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        }

        // 获取一个月的时间
        Calendar calendar = Calendar.getInstance();
        String endDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        Long endTime = calendar.getTimeInMillis();
        calendar.add(Calendar.MONTH, -1);
        String startDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");

        List<String> xData = new ArrayList<>();
        while (calendar.getTimeInMillis() < endTime) {
            xData.add(DateUtil.format(calendar.getTime(), "MM.dd"));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        // 将时间进行周一到周日补全
        calendar = Calendar.getInstance();
        // 将结束时间设置为周日
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            calendar.add(Calendar.WEEK_OF_MONTH, -1);
        }
        calendar.add(Calendar.WEEK_OF_MONTH, 1);
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        String fillEndDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        Long fillEndTime = calendar.getTimeInMillis();

        calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            calendar.add(Calendar.WEEK_OF_MONTH, -1);
        }
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        String fillStartDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");

        List<String> fillXData = new ArrayList<>();
        while (calendar.getTimeInMillis() <= fillEndTime) {
            fillXData.add(DateUtil.format(calendar.getTime(), "MM.dd"));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        // 获取预警信息
        List<WarningDto> warningList = alarmmsgMapper.findByCode(project.getCode(), startDate, endDate);
        for (WarningDto dto : warningList) {
            StringBuffer stringBuffer = new StringBuffer();
            if (dto.getAlarmDj().equals("I"))
                stringBuffer.append("一级预警");
            else if (dto.getAlarmDj().equals("II"))
                stringBuffer.append("二级预警");
            else if (dto.getAlarmDj().equals("III"))
                stringBuffer.append("三级预警");
            else if (dto.getAlarmDj().equals("IV"))
                stringBuffer.append("四级预警");
            stringBuffer.append(" ");
            stringBuffer.append(dto.getCreatedTime());
            stringBuffer.append(" ");
            stringBuffer.append(dto.getAlarmName());
            stringBuffer.append(" ");
            stringBuffer.append(dto.getJdzb());
            stringBuffer.append(dto.getAlarmMax());
            dto.setContent(stringBuffer.toString());
        }
        staMap.setWarningList(warningList);

        // 获取监测变化数据
        // 台站对列表
        String str = project.getProjectGstacal();
        // 将台站号替换为台站别名
        String nameSta = project.getProjectNamesta().toUpperCase();
        Map<String, String> nameStaMap = new HashMap<>();
        if (StrUtil.isNotEmpty(nameSta)) {
            String[] strs = nameSta.split(";");
            for (String key : strs) {
                String[] ss = key.split("-");
                nameStaMap.put(ss[0], ss[1]);
            }
        }
        MonitorDataDto monitorDataDto = new MonitorDataDto();
        monitorDataDto.setEndDate(fillEndDate);
        monitorDataDto.setStartDate(fillStartDate);
        monitorDataDto.setXData(fillXData);
        // 查找时间段内，该项目的数据
        List<Monitordata> fillMonitordataList = monitordataMapper.findByCode(project.getCode(), fillStartDate, fillEndDate);
        List<Monitordata> monitordataList = monitordataMapper.findByCode(project.getCode(), startDate, endDate);
        if (StrUtil.isNotEmpty(str)) {
            String[] strs = str.split(";");
            List<Map<String, String>> list = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            for (String key : strs) {
                String[] s1 = key.split(":");
                if (s1.length == 2) {
                    String[] s2 = s1[0].split("-");
                    if (s2.length == 2) {
                        String n1 = (nameStaMap.get(s2[0]) != null ? nameStaMap.get(s2[0]) : s2[0])
                                + "-"
                                + (nameStaMap.get(s2[1]) != null ? nameStaMap.get(s2[1]) : s2[1]);
                        String n2 = "监测变化";
                        Map map = new HashMap();
                        map.put("staName", n1);
                        map.put("nameDes", n2);
                        list.add(map);
                        list2.add((nameStaMap.get(s2[0]) != null ? nameStaMap.get(s2[0]) : s2[0])
                                + "-"
                                + (nameStaMap.get(s2[1]) != null ? nameStaMap.get(s2[1]) : s2[1])
                                + "监测变化");
                    } else if (s2.length == 1) {
                        String n1 = (nameStaMap.get(s2[0]) != null ? nameStaMap.get(s2[0]) : s2[0]);
                        if (n1.equals("ACHL"))
                            continue;
                        String n2 = "监测变化";
                        Map map = new HashMap();
                        map.put("staName", n1);
                        map.put("nameDes", n2);
                        list.add(map);
                        list2.add((nameStaMap.get(s2[0]) != null ? nameStaMap.get(s2[0]) : s2[0])
                                + "监测变化");
                    }
                }
            }
            monitorDataDto.setGstacalList(list2);
            monitorDataDto.setGstacalListNameAndDes(list);

            if (list.size() > 0) {
                // 默认加载第一个台站对数据
                String gstacalName = list2.get(0);
                String nameDes = list.get(0).get("nameDes");
                String name = list.get(0).get("staName");
                monitorDataDto.setName(gstacalName);
                monitorDataDto.setNameDes(nameDes);
                monitorDataDto.setGstacalName(name);
            }
            // 获取vvig: t01_t02:-0.14;t01_t03:-0.17;t01_t04:-0.19;t02_t03:-0.05;t02_t04:-0.17;t03_t04:-0.07;
            List<String> yVvigList = new ArrayList<>();
            List<String> yKdigList = new ArrayList<>();
            MonitorDataDto.MonitorYData monitorVvig = new MonitorDataDto.MonitorYData();
            MonitorDataDto.MonitorYData monitorKdig = new MonitorDataDto.MonitorYData();

            calendar.setTime(DateUtil.parseDate(fillStartDate));
            while (calendar.getTimeInMillis() <= fillEndTime) {
                Boolean has = false;
                for (Monitordata monitordata : fillMonitordataList) {
                    String d = DateUtil.formatDate(calendar.getTime());
                    if (monitordata.getDate().contains(d)) {
                        has = true;
                        // 遍历数据，解析vvig数据
                        String vvig = monitordata.getVvig();
                        String[] vvigs = vvig.split(";");
                        for (String key : vvigs) {
                            //  对比数据中台站对别名和 本次查找的台站对别名是否对应
                            String[] vs = key.split(":");
                            String name = monitorDataDto.getName();
                            String[] names = name.split(":");
                            String[] v1s = vs[0].split("_");
                            String[] v2s = names[0].split("-");
                            if (v2s.length == 2 && (
                                    (v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1]))
                            )) {
                                if (vs[1].compareTo("100") > 0)
                                    yVvigList.add(null);
                                else
                                    yVvigList.add(vs[1]);
                                break;
                            } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                                if (vs[1].compareTo("100") > 0)
                                    yVvigList.add(null);
                                else
                                    yVvigList.add(vs[1]);
                                break;
                            }
                        }

                        // 解析kdig数据
                        String kdig = monitordata.getKdig();
                        String[] kdigs = kdig.split(";");
                        for (String key : kdigs) {
                            String[] vs = key.split(":");
                            String name = monitorDataDto.getName();
                            String[] names = name.split(":");
                            String[] v1s = vs[0].split("_");
                            String[] v2s = names[0].split("-");
                            if (v2s.length == 2 && ((v2s[0].contains(v1s[0]) || v2s[0].contains(v1s[1])) && (v2s[1].contains(v1s[0]) || v2s[1].contains(v1s[1])))) {
                                if (vs[1].compareTo("100") > 0)
                                    yKdigList.add(null);
                                else
                                    yKdigList.add(vs[1]);
                                break;
                            } else if (v2s.length == 1 && v2s[0].contains(v1s[0])) {
                                if (vs[1].compareTo("100") > 0)
                                    yKdigList.add(null);
                                else
                                    yKdigList.add(vs[1]);
                                break;
                            }
                        }
                    }
                }
                if (!has) {
                    yVvigList.add(null);
                    yKdigList.add(null);
                }
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            monitorVvig.setYData(yVvigList);
            monitorKdig.setYData(yKdigList);

            monitorDataDto.setVvig(monitorVvig);
            monitorDataDto.setKdig(monitorKdig);
        }

        // 获取简介
        String des = project.getDescription();

        // 获取灾害指数
        Monitordata monitordataLast = monitordataMapper.findLastByCode(project.getCode());
        String disasterExponential = monitordataLast != null ? monitordataLast.getAlarmval() : "--";

        // 获取核心检测数据
        CoreMonitorDataDto coreMonitorDataDto = new CoreMonitorDataDto();
        coreMonitorDataDto.setEndDate(fillEndDate);
        coreMonitorDataDto.setStartDate(fillStartDate);
        coreMonitorDataDto.setXData(fillXData);
        MonitorDataDto.MonitorYData monitorTem = new MonitorDataDto.MonitorYData();
        MonitorDataDto.MonitorYData monitorRainfall = new MonitorDataDto.MonitorYData();
        MonitorDataDto.MonitorYData monitorPgv = new MonitorDataDto.MonitorYData();
        MonitorDataDto.MonitorYData monitorAvvig = new MonitorDataDto.MonitorYData();
        MonitorDataDto.MonitorYData monitorAkdig = new MonitorDataDto.MonitorYData();
        MonitorDataDto.MonitorYData monitorHumity = new MonitorDataDto.MonitorYData();
        List<String> yTemList = new ArrayList<>();
        List<String> yRainfallList = new ArrayList<>();
        List<String> yPgvList = new ArrayList<>();
        List<String> yAvvigList = new ArrayList<>();
        List<String> yAkdigList = new ArrayList<>();
        List<String> yHumityList = new ArrayList<>();

        calendar.setTime(DateUtil.parseDate(fillStartDate));
        while (calendar.getTimeInMillis() <= fillEndTime) {
            Boolean has = false;
            for (Monitordata monitordata : fillMonitordataList) {
                String d = DateUtil.formatDate(calendar.getTime());
                if (monitordata.getDate().contains(d)) {
                    has = true;
                    // 遍历数据，解析vvig数据
                    String overalldd = monitordata.getOveralldd();
                    String[] overalldds = overalldd.split(";");
                    for (String key : overalldds) {
                        String[] vs = key.split(":");
                        if (vs[0].equals("Rain"))
                            yRainfallList.add(vs[1]);
                        else if (vs[0].equals("temperatue"))
                            yTemList.add(vs[1]);
                        else if (vs[0].equals("PGV"))
                            yPgvList.add(vs[1]);
                        else if (vs[0].equals("humity"))
                            yHumityList.add(vs[1]);
                        //Rain:2.2;temperatue:5.17;PGV:2.5;Pressure:91.27;humity:68.05;lamination:58318;PGA:5.1;
                    }
                    String avvig = monitordata.getAvvig();
                    if (StrUtil.isNotEmpty(avvig)) {
                        avvig = avvig.trim();
                        if (avvig.endsWith(";"))
                            avvig = avvig.substring(0, avvig.length() - 1);
                        String[] avvigs = avvig.split(":");
                        if (avvigs.length == 2) {
                            if (avvigs[1].compareTo("100") > 0)
                                yAvvigList.add(null);
                            else
                                yAvvigList.add(avvigs[1]);
                        }
                    }

                    String akdig = monitordata.getAkdig();
                    if (StrUtil.isNotEmpty(akdig)) {
                        akdig = akdig.trim();
                        if (akdig.endsWith(";"))
                            akdig = akdig.substring(0, akdig.length() - 1);
                        String[] akdigs = akdig.split(":");
                        if (akdigs.length == 2) {
                            if (akdigs[1].compareTo("100") > 0)
                                yAvvigList.add(null);
                            else
                                yAkdigList.add(akdigs[1]);
                        }
                    }
                }
            }
            if (!has) {
                yRainfallList.add(null);
                yTemList.add(null);
                yPgvList.add(null);
                yAvvigList.add(null);
                yAkdigList.add(null);
                yHumityList.add(null);
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        monitorTem.setYData(yTemList);
        monitorAkdig.setYData(yAkdigList);
        monitorAvvig.setYData(yAvvigList);
        monitorPgv.setYData(yPgvList);
        monitorRainfall.setYData(yRainfallList);
        monitorHumity.setYData(yHumityList);
        coreMonitorDataDto.setAkdig(monitorAkdig);
        coreMonitorDataDto.setAvvig(monitorAvvig);
        coreMonitorDataDto.setTem(monitorTem);
        coreMonitorDataDto.setPgv(monitorPgv);
        coreMonitorDataDto.setRainfall(monitorRainfall);
        coreMonitorDataDto.setHumity(monitorHumity);

        // 获取系统运行数据
        ServicemDataDto servicemDataDto = new ServicemDataDto();
        calendar = Calendar.getInstance();
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 2)
            calendar.add(Calendar.WEEK_OF_MONTH, -1);

        calendar.set(Calendar.DAY_OF_WEEK, 2);
        startDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        calendar.add(Calendar.WEEK_OF_MONTH, 1);
        endDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");

        BigDecimal totalAvvig = new BigDecimal(0);
        BigDecimal maxAvvig = new BigDecimal(0);
        BigDecimal minAvvig = new BigDecimal(0);

        List<Monitordata> monitordataListSystem = monitordataMapper.findByCode(project.getCode(), startDate, endDate);
        if (monitordataListSystem.size() > 0) {
            BigDecimal pressureTotal = new BigDecimal(0);
            BigDecimal tmpTotal = new BigDecimal(0);
            BigDecimal humidityTotal = new BigDecimal(0);
            BigDecimal illuminationTotal = new BigDecimal(0);
            BigDecimal rainfallTotal = new BigDecimal(0);
            int temperatueNum = monitordataListSystem.size();
            int rainNum = monitordataListSystem.size();
            int pressureNum = monitordataListSystem.size();
            int humityNum = monitordataListSystem.size();
            int laminationNum = monitordataListSystem.size();
            for (Monitordata monitordata : monitordataListSystem) {
                String overalldd = monitordata.getOveralldd();
                String[] overalldds = overalldd.split(";");
                for (String key : overalldds) {
                    String[] vs = key.split(":");
                    if (vs[0].equals("Rain")) {
                        if (vs[1].contains("#QO")) {
                            rainNum--;
                        } else
                            rainfallTotal = rainfallTotal.add(new BigDecimal(vs[1]));
                    } else if (vs[0].equals("temperatue")) {
                        if (vs[1].contains("#QO")) {
                            temperatueNum--;
                        } else
                            tmpTotal = tmpTotal.add(new BigDecimal(vs[1]));
                    } else if (vs[0].equals("Pressure")) {
                        if (vs[1].contains("#QO")) {
                            pressureNum--;
                        } else
                            pressureTotal = pressureTotal.add(new BigDecimal(vs[1]));
                    } else if (vs[0].equals("humity")) {
                        if (vs[1].contains("#QO")) {
                            humityNum--;
                        } else
                            humidityTotal = humidityTotal.add(new BigDecimal(vs[1]));
                    } else if (vs[0].equals("lamination")) {
                        if (vs[1].contains("#QO")) {
                            laminationNum--;
                        } else
                            illuminationTotal = illuminationTotal.add(new BigDecimal(vs[1]));
                    }
                    //Rain:2.2;temperatue:5.17;PGV:2.5;Pressure:91.27;humity:68.05;lamination:58318;PGA:5.1;
                }
                String avvig = monitordata.getAvvig();
                if (StrUtil.isNotEmpty(avvig)) {
                    avvig = avvig.trim();
                    if (avvig.endsWith(";"))
                        avvig = avvig.substring(0, avvig.length() - 1);
                    String[] avvigs = avvig.split(":");
                    if (avvigs.length == 2) {
                        BigDecimal b = new BigDecimal(avvigs[1]);
                        totalAvvig = totalAvvig.add(b);
                        if (maxAvvig.compareTo(b) < 0 || maxAvvig.compareTo(new BigDecimal(0)) == 0)
                            maxAvvig = b;
                        if (minAvvig.compareTo(b) > 0 || minAvvig.compareTo(new BigDecimal(0)) == 0)
                            minAvvig = b;
                    }
                }
            }
            servicemDataDto.setIllumination(illuminationTotal.divide(new BigDecimal(laminationNum > 0 ? laminationNum : 1), RoundingMode.HALF_DOWN).setScale(1, BigDecimal.ROUND_HALF_DOWN) + "LUX");
            servicemDataDto.setHumidity(humidityTotal.divide(new BigDecimal(humityNum > 0 ? humityNum : 1), RoundingMode.HALF_DOWN).setScale(1, BigDecimal.ROUND_HALF_DOWN) + "%");
            servicemDataDto.setPressure(pressureTotal.divide(new BigDecimal(pressureNum > 0 ? pressureNum : 1), RoundingMode.HALF_DOWN).setScale(1, BigDecimal.ROUND_HALF_DOWN) + "kPa");
            servicemDataDto.setRainfall(rainfallTotal.setScale(2, BigDecimal.ROUND_HALF_DOWN) + "mm");
            servicemDataDto.setTmp(tmpTotal.divide(new BigDecimal(temperatueNum > 0 ? temperatueNum : 1), RoundingMode.HALF_DOWN).setScale(1, BigDecimal.ROUND_HALF_DOWN) + "℃");
        }

        // 获取灾害简报
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("本周平均波速变化为");
        if (monitordataListSystem.size() > 0)
            stringBuffer.append(totalAvvig.divide(new BigDecimal(monitordataListSystem.size()), RoundingMode.HALF_DOWN).setScale(2, BigDecimal.ROUND_HALF_DOWN));
        else
            stringBuffer.append("0");
        stringBuffer.append("%，最低值为");
        stringBuffer.append(minAvvig);
        stringBuffer.append("%，最高值为");
        stringBuffer.append(maxAvvig);
        stringBuffer.append("%。综合降雨、气温等重要数据，系统判断边坡灾害为");
        stringBuffer.append(disasterExponential);

        String disaster = stringBuffer.toString()
                .replaceAll("middle_low", "中低风险")
                .replaceAll("low", "低风险")
                .replaceAll("middle", "中风险")
                .replaceAll("high", "高风险");

        mainIndexDto.setDes(des);
        mainIndexDto.setCoreMonitorData(coreMonitorDataDto);
        mainIndexDto.setDisasterExponential(disasterExponential);
        mainIndexDto.setDisasterReport(disaster);
        mainIndexDto.setMap(staMap);
        mainIndexDto.setMonitor(monitorDataDto);
        mainIndexDto.setMonitoringSite(monitoringSite);
        mainIndexDto.setStaList(staDataList);
        mainIndexDto.setServicemData(servicemDataDto);


        return new ResponseResult().success().setData(mainIndexDto);
    }

    public static void main(String[] strs) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDateTime("2021-09-21 10:24:11"));
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 2)
            calendar.add(Calendar.WEEK_OF_MONTH, -1);

        calendar.set(Calendar.DAY_OF_WEEK, 2);
        String startDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
        calendar.add(Calendar.WEEK_OF_MONTH, 1);
        String endDate = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");

        System.out.println("endDate:" + endDate);
        System.out.println("startDate:" + startDate);
    }

    public ResponseResult<List<SimProjectListDto>> projectList(LoginParam param) {
        if (tokenValid) {
            // 验证token
            Integer userId = TokenUtil.verifyToken();

            Map<String, Claim> map = TokenUtil.verifyAccessToken(param.getAccessToken());
            if (!map.containsKey("auth"))
                throw BizException.fail(E_EXCEPTION_TYPE.access_token_invalid);
            JSONObject data = JSONUtil.parseObj(map.get("auth").asString());
            if (!data.containsKey("user"))
                throw BizException.fail(E_EXCEPTION_TYPE.access_token_invalid);
            if (!data.containsKey("projectList"))
                throw BizException.fail(E_EXCEPTION_TYPE.has_not_authority);
            JSONObject jsonObject = data.getJSONObject("user");
            String manageProjectCodes = jsonObject.getStr("manageProjectCodes");
            String[] strs = manageProjectCodes.split(";");

            List<SimProjectListDto> list = new ArrayList<>();
            for (String key : strs) {
                Project project = projectMapper.findByCode(key);
                if (project != null && project.getProjectPen() != 0) {
                    SimProjectListDto projectListDto = new SimProjectListDto();
                    projectListDto.setProjectCode(project.getCode());
                    projectListDto.setProjectName(project.getName());
                    list.add(projectListDto);
                }
            }

            return new ResponseResult<List<SimProjectListDto>>().setData(list);
        } else {
            List<SimProjectListDto> list = projectMapper.findAllSimple();
            return new ResponseResult<List<SimProjectListDto>>().setData(list);
        }
    }
}
